package com.cn.forum.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.baijia.tianxiao.sqlbuilder.SingleSqlBuilder;
import com.baijia.tianxiao.sqlbuilder.support.JdbcTemplateDaoSupport;
import com.cn.forum.dao.ArticleDao;
import com.cn.forum.model.Article;
import com.google.common.collect.Maps;


@Repository
public class ArticleDaoImpl extends JdbcTemplateDaoSupport<Article> implements ArticleDao {

	@Override
	public List<Article> getPageArticle(int pageNum, int pageSize) {
		// rTODO Auto-generated method stubw
		int fromIndex=pageSize*(pageNum-1);
		Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("fromIndex", fromIndex);
        paramMap.put("pageSize", pageSize);
        
        
		String querySql = "select id,content,user_id,user_name,parent_id from MyForum.Article where parent_id is Null order by id desc limit :fromIndex,:pageSize ";
        List<Article> list = this.getNamedJdbcTemplate().query(querySql, paramMap, new RowMapper<Article>(){
			@Override
			public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				Article ar = new Article();
				ar.setId(rs.getInt("id"));
				ar.setContent(rs.getString("content"));
				ar.setUserid(rs.getInt("user_id"));
				ar.setUsername(rs.getString("user_name"));
				ar.setParentId(rs.getInt("parent_id"));
				return ar;
			}
        } );
        return list;
	}

	@Override
	public List<Article> getChildrenArticle(List<Article> articleList) {
		// TODO Auto-generated method stub
		List<Integer> parentId=new ArrayList<Integer>();
		for(Article a:articleList){
			Integer PI=a.getId();
			parentId.add(PI);
		}
		if(parentId.isEmpty()){
			return new ArrayList<Article>();
		}
		
		
		Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("parentId", parentId);
        
		String querySql = "select id,content,user_id,user_name,parent_id from MyForum.Article where parent_id in (:parentId)";
        List<Article> list = this.getNamedJdbcTemplate().query(querySql, paramMap, new RowMapper<Article>(){
			@Override
			public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				Article ar = new Article();
				ar.setId(rs.getInt("id"));
				ar.setContent(rs.getString("content"));
				ar.setUserid(rs.getInt("user_id"));
				ar.setUsername(rs.getString("user_name"));
				ar.setParentId(rs.getInt("parent_id"));
				return ar;
			}
        } );
        return list;	
        }

	@Override
	public Integer getMainArticleNum() {
		// TODO Auto-generated method stub
		
		String sql="SELECT COUNT(*) FROM MyForum.Article where `parent_id` is NULL";
		Map<String, Object> map = new HashMap<>();
		Integer result=this.getNamedJdbcTemplate().queryForObject(sql, map, Integer.class);
		return result;
	}

    @Override
	public List<Article> getPageMyArticle(String myName,Integer pageNum,Integer pageSize) {
		// TODO Auto-generated method stub
/*      这个是返回所有该用户发的贴。先注释掉      
 *      SingleSqlBuilder<Article> builder = createSqlBuilder();
        builder.eq("username", myName);
    	return queryList(builder);*/	
		int fromIndex=pageSize*(pageNum-1);
		Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("fromIndex", fromIndex);
        paramMap.put("pageSize", pageSize);
        paramMap.put("username", myName);
		String querySql = "select id,content,user_id,user_name,parent_id from MyForum.Article where user_name=:username order by id desc limit :fromIndex,:pageSize ";
        List<Article> list = this.getNamedJdbcTemplate().query(querySql, paramMap, new RowMapper<Article>(){
			@Override
			public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				Article ar = new Article();
				ar.setId(rs.getInt("id"));
				ar.setContent(rs.getString("content"));
				ar.setUserid(rs.getInt("user_id"));
				ar.setUsername(rs.getString("user_name"));
				ar.setParentId(rs.getInt("parent_id"));
				return ar;
			}
        } );
        return list;
	}

    
	@Override
	public Integer getMyArticleNum(String userName) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userName", userName);
		String sql="SELECT COUNT(*) FROM MyForum.Article where `user_name` =:userName ";
		Map<String, Object> map = new HashMap<>();
		Integer result=this.getNamedJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
		return result;	}
}
